package com.yuezhu.global;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Button;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.yuezhu.Model.UserInfoModel;
import com.yuezhu.R;
import com.yuezhu.activity.LoginActivity;
import com.yuezhu.activity.MyprojectActivity;
import com.yuezhu.been.UserMsg;
import com.yuezhu.control.TaskEngine;
import com.yuezhu.util.LoginOut;
import com.yuezhu.util.MyToast;
import com.yuezhu.util.Preference;
import com.yuezhu.util.StringUtil;

import org.json.JSONObject;


/**
 * @创建人：zhaoshuzhen
 * @创建时间：2015/12/8 16:24
 */
public class StartActivity extends BaseActivity {
    private Preference preference;
    private Context context;
    private Button mIvStart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.start_activity_layout);
        context = this;
        /**
         * 禁止默认的页面统计方式，这样将不会再自动统计Activity。
         */
//		MobclickAgent.openActivityDurationTrack(false);
        preference = Preference.instance(this);
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                initView();
            }
        }, 1500);

    }


    @Override
    protected void initView() {
        super.initView();
        jumpActivity();

    }

    /**
     * 启动页面界面跳转
     */
    private void jumpActivity() {
        String code = preference.getString("authToken");
        if (StringUtil.isEmpty(code)) {
            Intent intent = new Intent();
            intent.setClass(context, LoginActivity.class);
            startActivity(intent);
            finish();
        } else {
            TaskEngine.getInstance().startGetUserInfoTask(queue, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject json) {
                    try {
                        String respCode = json.getString("respCode");
                        if (!StringUtil.equal(respCode, "OK")) {
                            try {
                                if (json.getInt("respNo") == 999) {
                                    LoginOut.LoginOut(StartActivity.this);
                                }
                            } catch (Exception e) {
                                MyToast.showDefaltToast("获取数据失败");
                            }
//                            Intent intent = new Intent();
//                            intent.setClass(context, MyprojectActivity.class);
//                            startActivity(intent);
//                            finish();
                        } else {
                            UserInfoModel model = new Gson().fromJson(json.getString("userInfo").toString(), UserInfoModel.class);
                            UserMsg.getInstance().setLevel(model.level);
                            Intent intent = new Intent();
                            intent.setClass(context, MyprojectActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        MyToast.showDefaltToast("获取数据失败");
                        Intent intent = new Intent();
                        intent.setClass(context, MyprojectActivity.class);
                        startActivity(intent);
                        finish();
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    MyToast.showDefaltToast("获取数据失败");
                    Intent intent = new Intent();
                    intent.setClass(context, MyprojectActivity.class);
                    startActivity(intent);
                    finish();
                }
            });

        }
    }

    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    public void onPause() {
        super.onPause();

    }

}
